package com.yexiao.cofe.business.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson2.JSONObject;
import com.yexiao.cofe.business.mapper.ParamItemMapper;
import com.yexiao.cofe.business.mapper.ParamValueMapper;
import com.yexiao.cofe.business.pojo.ParamItem;
import com.yexiao.cofe.business.pojo.ParamValue;
import com.yexiao.cofe.business.service.ParamItemService;
import com.yexiao.cofe.common.exception.BusinessException;
import com.yexiao.core.util.JSONUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lkl
 * @since 2025/3/31
 */
@Service
public class ParamItemServiceImpl implements ParamItemService {

    @Resource
    private ParamItemMapper paramItemMapper;
    @Resource
    private ParamValueMapper paramValueMapper;

    @Override
    public void addParamItem(JSONObject param) {
        String name = JSONUtil.checkAndGetStr(param, "name");
        Integer type = JSONUtil.checkAndGetInt(param, "type");
        Integer isRequired = JSONUtil.checkAndGetInt(param, "isRequired");
        Integer sort = JSONUtil.checkAndGetInt(param, "sort");

        ParamItem paramItem = paramItemMapper.selectByName(name);
        if(paramItem != null) {
            throw new BusinessException("参数项已存在");
        }

        ParamItem newParamItem = new ParamItem();
        newParamItem.setName(name);
        newParamItem.setType(type);
        newParamItem.setIsRequired(isRequired);
        newParamItem.setSort(sort);
        paramItemMapper.insert(newParamItem);
    }

    @Override
    public List<JSONObject> listAllParamItem() {
        List<ParamItem> paramItemList = paramItemMapper.listAll();
        List<ParamValue> paramValueList = paramValueMapper.listAll();

        List<JSONObject> list = new ArrayList<>();
        for (ParamItem paramItem : paramItemList) {
            JSONObject baseMsg = ParamItem.getParamItemBaseMsg(paramItem);
            baseMsg.put("child", getParamValueList(paramItem.getId(), paramValueList));
            list.add(baseMsg);
        }

        return list;
    }

    @Override
    public JSONObject getParamItemDetail(Integer paramItemId) {
        ParamItem paramItem = paramItemMapper.selectById(paramItemId);
        if(paramItem == null) {
            throw new BusinessException("参数项不存在");
        }

        return ParamItem.getParamItemBaseMsg(paramItem);
    }

    @Override
    public void deleteParamItem(JSONObject param) {
        Integer id = JSONUtil.checkAndGetInt(param, "id");

        // 批量删除绑定的参数值
        List<ParamValue> paramValues = paramValueMapper.listByParamItemId(id);
        if(CollectionUtil.isNotEmpty(paramValues)) {
            List<Integer> paramValueIds = paramValues.stream().map(ParamValue::getId).collect(Collectors.toList());
            paramValueMapper.deleteByIds(paramValueIds);
        }

        paramItemMapper.deleteById(id);
    }

    @Override
    public void modifyParamItem(JSONObject param) {
        Integer id = JSONUtil.checkAndGetInt(param, "id");
        String name = JSONUtil.getStr(param, "name");
        Integer type = JSONUtil.getInt(param, "type");
        Integer isRequired = JSONUtil.getInt(param, "isRequired");
        Integer sort = JSONUtil.getInt(param, "sort");

        ParamItem paramItem = paramItemMapper.selectById(id);
        if(paramItem == null) {
            throw new BusinessException("参数项不存在");
        }

        paramItemMapper.modifyParamItem(id, name, type, isRequired, sort);
    }

    private List<JSONObject> getParamValueList(int paramItemId, List<ParamValue> paramValueList) {
        List<JSONObject> list = new ArrayList<>();
        for (ParamValue paramValue : paramValueList) {
            if(paramValue.getParamItemId() == paramItemId) {
                list.add(ParamValue.getBaseMsg(paramValue));
            }
        }

        return list;
    }
}
